package com.ltcode.sys.interfaces.feign;

import cn.hutool.json.JSONUtil;
import com.ltcode.common.base.web.domain.ApiResponse;
import com.ltcode.sys.application.service.SysPermissionAppService;
import com.ltcode.sys.application.service.SysUserAppService;
import com.ltcode.sys.domain.model.SysUser;
import com.ltcode.sys.interfaces.feign.model.LoginUser;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Set;

/**
 * @Author： ltcode
 * @Create by： 2025/7/30 0030 21:33
 * @ClassName: SysUserFeignController
 * @Describe：
 */
@RestController
@RequestMapping("/feign/user")
@AllArgsConstructor
public class SysUserFeignController {
    private final SysUserAppService userService;
    private final SysPermissionAppService permissionService;;
    /**
     * 根据用户编号获取详细信息
     */
    @GetMapping(value = "/getInfo/id/{userId}")
    public ApiResponse<String> getInfo(@PathVariable(value = "userId", required = false) Long userId) {
        userService.checkUserDataScope(userId);
        SysUser user = userService.selectUserById(userId);
        return ApiResponse.ok(JSONUtil.toJsonStr(user));
    }

    /**
     * 根据用户编号获取详细信息
     */
    @GetMapping(value = { "/", "/getInfoByUserName/{userName}" })
    public ApiResponse<LoginUser> getInfoByUserName(@PathVariable(value = "userName", required = false) String userName) {
        SysUser user = userService.selectUserByUserName(userName);
        LoginUser loginUser = new LoginUser();
        loginUser.setSysUser(user);
        //获取权限角色信息
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);

        loginUser.setPermissions(permissions);
        loginUser.setRoles(roles);
        return ApiResponse.ok(loginUser);
    }


    @PostMapping("/register")
    public ApiResponse<Boolean> register(@RequestBody SysUser sysUser)
    {
        userService.registerUser(sysUser);
        return ApiResponse.ok();
    }
}
